package cn.tedu;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        try (
                Connection conn = DBUtils.getConn();)
        {
            //创建执行SQL语句的对象
            Statement s = conn.createStatement();
            //查询用户是否存在
            ResultSet rs = s.executeQuery("select id from user where username='" + username + "'");
            if(rs.next()){
                System.out.println("用户已存在");
                return;
            }
            String sql = "insert into user values(null,'"+username+"','"+password+"')";
//            s.execute("insert into user values (null,'"+username+"','"+password+"')");
            System.out.println(sql);
            s.execute(sql);
            System.out.println("注册完毕");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
